<template>
  <section id="author">
    <el-card class="" shadow="never">
      <div slot="header">
        <span class="has-text-weight-bold">👨‍💻 关于作者</span>
      </div>
      <div class="has-text-centered">
        <p class="is-size-5 mb-5">
          <router-link :to="{ path: `/home/member/${Topuser.username}/homeIndex` }">
            {{ Topuser.alias }} <span class="is-size-7 has-text-grey">{{ '@' + Topuser.username }}</span>
          </router-link>
        </p>
        <div class="columns is-mobile">
          <div class="column is-half">
            <code>{{ Topuser.topicCount }}</code>
            <p>文章</p>
          </div>
          <div class="column is-half">
            <code>{{ Topuser.followerCount }}</code>
            <p>粉丝</p>
          </div>
        </div>
        <div>
          <button
            v-if="hasFollow"
            class="button is-success button-center is-fullwidth"
            @click="handleUnFollow(Topuser.id)"
          >
            已关注
          </button>
          <button v-else class="button is-link button-center is-fullwidth" @click="handleFollow(Topuser.id)">
            关注
          </button>
        </div>
      </div>
    </el-card>
  </section>
</template>

<script>
import { follow, hasFollow, unFollow } from '@/api/follow'
import { mapGetters } from 'vuex'
// import request from '../../utils/request'
export default {
  name: 'Author',
  props: {
    Topuser: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      hasFollow: false,
      hasYouself: false
    }
  },
  mounted () {
    this.fetchInfo()
    // console.log(this.Topuser.id) // 作者的id
  },
  computed: {
    ...mapGetters([
      'token', 'user'
    ])
  },
  methods: {
    fetchInfo () {
      // console.log(this.user.id) // 登陆用户的id
      // console.log(this.Topuser.id) // 文字作者的id
      if (this.token != null && this.token !== '') {
        hasFollow(this.Topuser.id).then(value => {
          const { data } = value
          console.log(data) // true false
          this.hasFollow = data.hasFollow
          console.log(this.user.id)
        })
      }
    },
    handleFollow: function (id) {
      if (this.user.id === this.Topuser.id) {
        this.$message.error('你不能关注你自己啊')
      } else if (this.token != null && this.token !== '') {
        follow(id).then(response => {
          const { message } = response
          this.$message.success(message)
          this.hasFollow = !this.hasFollow
          this.Topuser.followerCount = parseInt(this.Topuser.followerCount) + 1
        }
        )
      } else {
        this.$message.success('请先登录')
      }
    },
    handleUnFollow: function (id) {
      unFollow(id).then(response => {
        const { message } = response
        this.$message.success(message)
        this.hasFollow = !this.hasFollow
        this.Topuser.followerCount = parseInt(this.Topuser.followerCount) - 1
      })
    }
  }
}
</script>

<style scoped>

</style>
